package cn.edu.njnu.service;

import cn.edu.njnu.pojo.Result;
import cn.edu.njnu.pojo.ResultFactory;
import cn.edu.njnu.util.DocProcessor;
import org.ansj.app.keyword.KeyWordComputer;
import org.ansj.app.keyword.Keyword;
import org.ansj.splitWord.Analysis;
import org.ansj.splitWord.analysis.DicAnalysis;
import org.ansj.splitWord.analysis.ToAnalysis;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@Service
public class DocumentService {
    @Autowired
    private DocProcessor docProcessor;
    public Result KGGenerate(MultipartFile file){
        try {
            String content = docProcessor.extractText(file);
            String title = file.getOriginalFilename();
            KeyWordComputer<DicAnalysis> kwc = new KeyWordComputer<>(10);
            List<Keyword> keywords = kwc.computeArticleTfidf(title, content);
            return ResultFactory.buildSuccessResult("文本读取成功",keywords);
        } catch (IOException ignored){
            return ResultFactory.buildFailResult("文件读取错误");
        } catch (Exception e) {
            return ResultFactory.buildFailResult(e.getMessage());
        }
    }
}
